package interview.otco2522;


import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        final String outputCaseOne = rank("12", recordCaseOne());
        if (!Objects.equals(outputCaseOne, "T6546 T5227")) {
            throw new RuntimeException("case one assertFailed");
        }

        final String outputCaseTwo = rank("1", recordCaseTwo());
        if (!Objects.equals(outputCaseTwo, "T8000 T9000 T7002 T7001 T2001")) {
            throw new RuntimeException("case two assertFailed");
        }
    }

    private static String rank(String month, List<String> records) {
        // put your code here

        // 数据转换
        List<Record> data = new ArrayList<>();
        for (String record : records) {
            data.add(convert(record));
        }

        // 过滤月份 -> 数据统计: 计算总时长, 停车次数
        Map<String, List<Record>> collect = data.stream()
                .filter(r -> month.equals(getMonth(r.date)))
                .collect(Collectors.groupingBy(Record::getId));

        // 数据统计: 计算总时长, 停车次数
        Map<String, Processed> map = new HashMap<>();

        for (Record item : data) {
            String id = item.id;
            if (map.get(id) == null) {
                Processed processed = new Processed();
                processed.id = item.id;
                processed.frequency = 1;
                processed.totalDuration = item.duration;
                map.put(id, processed);
            } else {
                Processed old = map.get(id);
                old.frequency ++;
                old.totalDuration += item.duration;
                map.put(id, old);  // ?
            }
        }

        // 数据排序
        Set<Map.Entry<String, Processed>> entries = map.entrySet();
        List<Processed> list = new ArrayList<>();
        for (Map.Entry<String, Processed> entry : entries) {
            list.add(entry.getValue());
        }

        // 倒序排序所以 b - a
        list.sort((a, b) -> {
            if (b.totalDuration != a.totalDuration) {
                return b.totalDuration - a.totalDuration;
            }

            if (b.frequency != a.frequency) {
                return b.frequency - a.frequency;
            }

            return a.id.compareTo(b.id);
        } );

        // 取出前五个
        StringBuilder builder = new StringBuilder();
        if (list.size() <= 5) {
            for (int i = 0; i < list.size(); i++) {
                if (i != list.size() - 1) {
                    builder.append(list.get(i).id + " ");
                } else {
                    builder.append(list.get(i).id);
                }
            }
        } else {
            for (int i = 0; i < 5; i++) {
                if (i != 4) {
                    builder.append(list.get(i).id + " ");
                } else {
                    builder.append(list.get(i).id);
                }
            }
        }

//        System.out.println(builder.toString());

        return builder.toString();
    }

    private static Record convert(String raw) {
        String[] parts = raw.split("\\s");
        Record record = new Record(parts[0], parts[1], Integer.parseInt(parts[2]));
        return record;
    }

    private static String getMonth(String date) {
        String[] parts = date.split("-");
        return "" + Integer.parseInt(parts[1]);
    }

    private static List<String> recordCaseOne() {
        final List<String> seeds = new ArrayList<>();
        seeds.add("T6546 2020-12-01 8");
        seeds.add("T6546 2020-12-30 120");
        seeds.add("T5227 2020-12-31 136");
        seeds.add("T6546 2020-12-02 8");
        seeds.add("T6546 2020-08-01 8");
        return seeds;
    }

    private static List<String> recordCaseTwo() {
        final List<String> seeds = new ArrayList<>();
        seeds.add("T9000 2019-01-31 10");
        seeds.add("T9000 2019-01-31 90");
        seeds.add("T8000 2019-01-30 1");
        seeds.add("T7001 2019-01-03 10");
        seeds.add("T7002 2019-01-03 5");
        seeds.add("T7002 2019-01-03 5");
        seeds.add("T8000 2019-01-03 99");
        seeds.add("T2001 2019-01-02 9");
        seeds.add("T2003 2019-01-02 9");
        seeds.add("T1000 2019-11-02 101");
        return seeds;
    }
}

class Record {
    public String id;
    public String date;
    public int duration;
    
    public int frequency = 1;

    public Record(String id, String date, int duration) {
        this.id = id;
        this.date = date;
        this.duration = duration;
    }

    public String getId() {
        return id;
    }
}


class Processed {
    public String id;
    public int totalDuration;  // 总时长
    public int frequency;  // 次数
}
